<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Created by PhpStorm.
 * User: fyk
 * Date: 2019/12/4
 * Time: 9:56
 */
class Properties extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('attachs_m');
        $this->load->model('properties_m');
        $this->load->model('propertiesCategory_m');
        $this->load->model('customer_m');
    }

    /**
     * 首页列表
     * @param int $page
     */
    public function index($page = 1)
    {
        $get    = $this->input->get();
        $search = $where = [];
        if (isset($get['properties_title']) && !empty($get['properties_title'])) {
            $search['keyword'] = $keyword = $get['properties_title'];
            $where             = "title like '%{$keyword}%'";
        }
        //分页
        $limit                      = 20;
        $config['uri_segment']      = 4;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->properties_m->total($where);
        $config['base_url']         = base_url('manage/Properties/index');
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $properties         = $this->properties_m->select($where, $limit, $start);
        if ($properties) {
            foreach ($properties as $key => $val) {
                if (!empty($val['category'])) {
                    $category                     = $this->propertiesCategory_m->select("id in ({$val['category']})");
                    $properties[$key]['category'] = implode(",", array_column($category, "category_name"));
                } else {
                    $properties[$key]['category'] = "--";
                }
            }
        }
        $data['search']     = $search;
        $data['properties'] = $properties;
        // pp($data);die;
        $this->load->view('properties/index', $data);
    }

    /**
     * 新增视图
     */
    public function add()
    {
        $data['pic_path'] = "[]";
        $data['pic_con']  = json_encode([]);
        $data['category'] = $this->propertiesCategory_m->select(['deleted' => 0]);
        $data['customer'] = $this->customer_m->select(['deleted' => 0]);//客服

        $this->load->view('properties/form', $data);
    }

    /**
     * 查询修改内容
     * @param null $properties_id
     */
    public function edit($properties_id = null)
    {
        $properties = $this->properties_m->find($properties_id);
        //pp($properties);die;
        if (!$properties) {
            show_message('参数错误！', site_url(X_URL . 'Properties/index'));
        }
        // 商品图片
        if (!empty($properties['image_access_key'])) {
            $image_access_key = $properties['image_access_key'];
            $pic              = $this->attachs_m->get_attachs_by_access($image_access_key);
            $pic_path         = "[";
            $pic_con          = [];
            $del_url          = base_url("index.php/upload/delete_img_batch");
            if (!empty($pic)) {
                foreach ($pic as $key => $value) {
                    $pic_path  .= "'" . base_url('uploads/wuye/' . $image_access_key . "/" . $value['file_name']) . "',";
                    $pic_con[] = [
                        'caption' => $value['file_name'],
                        'size'    => 576237,
                        'width'   => '120px',
                        'url'     => $del_url,
                        "key"     => $value['id'],
                    ];
                }
                $data['pic_path'] = $pic_path . "]";
                $data['pic_con']  = json_encode($pic_con);
            } else {
                $data['pic_path'] = "[]";
                $data['pic_con']  = json_encode([]);
            }
        } else {
            $data['pic_path'] = "[]";
            $data['pic_con']  = json_encode([]);
        }
        $data['properties']    = $properties;
        $data['properties_id'] = $properties['id'];
        $data['category']      = $this->propertiesCategory_m->select(['deleted' => 0]);
        $data['customer']      = $this->customer_m->select(['deleted' => 0]);//客服

        $this->load->view('properties/form', $data);
    }

    /**
     * 删除
     * @param $properties_id
     */
    public function del($properties_id)
    {
        if ($properties_id) {
            if ($this->properties_m->delete_by(['id' => $properties_id])) {
                show_message('恭喜，删除成功', site_url(X_URL . 'Properties/index'), 1);
            } else {
                show_message('参数错误', site_url(X_URL . 'Properties/index'));
            }
        } else {
            show_message('参数错误', site_url(X_URL . 'Properties/index'));
        }
    }

    /**
     * 新增、修改
     */
    public function update()
    {

        $post = $this->input->post();
      //  pp($post);die;
        //
        $properties_title = isset($post['title']) ? htmlspecialchars($post['title'], ENT_QUOTES) : false;
        $properties_id    = isset($post['properties_id']) ? intval($post['properties_id']) : false;
        $properties       = [
            'title'            => $properties_title,
            'category'         => (isset($post['category']) && !empty($post['category'])) ? implode(",", $post['category']) : 0,
            'customer_id'      => (isset($post['customer_id']) && !empty($post['customer_id'])) ? implode(",", $post['customer_id']) : 0,
            'type'             => $post['type'] ?? "",
            'describe'         => $post['describe'] ?? "",
            'author'           => $post['author'] ?? "",
            'image_access_key' => $post['image_access_key'] ?? "",
            'content'          => $post['content'] ?? "",
            'address'          => $post['address'] ?? "",
            'total_hands'      => $post['total_hands'] ?? "",
            'price'            => $post['price'] ?? 0,
            'area'             => $post['area'] ?? "",
            'property'         => $post['property'] ?? "",
            'year'             => $post['year'] ?? "",
            'rate'             => $post['rate'] ?? "",
            'noi'              => $post['noi'] ?? "",
            'auction'          => $post['auction'] ?? 2,
            'real'             => $post['real'] ?? 2,
            'distressed'       => $post['distressed'] ?? 2,
            'sell'             => $post['sell'] ?? 2,
            'longitude'        => (!empty($post['longitude'])) ? $post['longitude'] : "116.40",
            'dimension'        => (!empty($post['dimension'])) ? $post['dimension'] : "39.90",
            'file_url'         => $post['file_url'] ?? "",
            'status'           => $post['status'] ?? 0,
        ];
        if (!$properties_id) {
            // 新增
            $properties['create_time'] = date("Y-m-d H:i:s", time());
            $properties['update_time'] = date("Y-m-d H:i:s", time());
            $properties                = $this->properties_m->insert($properties);
            if ($properties) {
                show_message('新增物业成功', site_url(X_URL . 'Properties/index'), 1);
            } else {
                show_message('新增物业失败', site_url(X_URL . 'Properties/add'));
            }
        } else {
            $properties['update_time'] = date("Y-m-d H:i:s", time());
            // 修改物业信息
            $affected_rows = $this->properties_m->update(['id' => $properties_id], $properties);
            if ($affected_rows) {
                show_message('修改物业成功', site_url(X_URL . 'Properties/index'), 1);
            } else {
                show_message('修改物业失败', site_url(X_URL . 'Properties/edit'));
            }
        }
    }

}


